<?php

//power by wshoto.com
if (!defined('IN_IA')) {
    die('Access Denied');
}
global $_W, $_GPC;

$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);

$level = intval($_GPC['level']) <= 4 ? intval($_GPC['level']) : '';

$commissionModel = p('commission');
$commissionSet = $commissionModel->getSet();

$member['commission'] = $commissionModel->getLevel($member['openid']);

if(empty($member['commission'])){
    $member['commission']['levelname'] = $commissionSet['levelname'];
}

switch($level){
    case 1 :
        $member['levelText'] = $commissionSet['texts']['myteam1'];
        break;
    case 2 :
        $member['levelText'] = $commissionSet['texts']['myteam2'];
        break;
    case 3 :
        $member['levelText'] = $commissionSet['texts']['myteam3'];
        break;
    case 4 :
        $member['levelText'] = $commissionSet['texts']['special'];
        break;
    default :
        $member['levelText'] = '其他会员';
        break;
}

$title = '我的 ' . $member['levelText'];

if ($_W['isajax']) {
    $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
    $psize = 10;

    $agentTeamsCount = $commissionModel->getCommissionTeamCount($openid, $level);

    $member['levelCount'] = !empty($agentTeamsCount[$level]) ? $agentTeamsCount[$level] : 0;

    $agentTeams = $commissionModel->getCommissionTeam($openid, $level, ($pindex - 1) * $psize, $psize);

    if (!empty($agentTeams)) {
        foreach ($agentTeams as &$row) {
            $recordPrice = $commissionModel->getCommissionRecordPrice($row['openid'], array('total'));
            $row['commission_total'] = !empty( $recordPrice['total'] ) ? number_format($recordPrice['total'], 2) : 0;

            $rowTeamsCount = $commissionModel->getCommissionTeamCount($row['openid'], $commissionSet['level']);
            $row['agentcount'] = !empty($rowTeamsCount['all']) ? intval($rowTeamsCount['all']) : 0;

            $row['agenttime'] = date('Y-m-d H:i', $row['agenttime']);
        }

        unset($row);
    }

    show_json(1, array('member' => $member, 'pagesize' => $psize, 'teams' => $agentTeams));
}

include $this->template('member/myVip');